package com.example.doctor.service.impl;

import com.example.common.entity.DoctorNotice;
import com.example.common.utils.IdUtils;
import com.example.doctor.dto.req.Add;
import com.example.doctor.dto.req.Delete;
import com.example.doctor.dto.req.Edit;
import com.example.doctor.dto.req.NoticeListReq;
import com.example.doctor.dto.resp.DoctorNoticeRsp;
import com.example.doctor.jpa.DoctorNoticeRepository;
import com.example.doctor.service.DoctorNoticeService;
import com.example.doctor.utils.UserUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author sifan
 * @PackagName:com.example.doctor.service.impl
 * @ClassName: DoctorNoticeServiceImpl
 * @Description:
 * @date:2021/1/19 16:47
 */
@Service
public class DoctorNoticeServiceImpl implements DoctorNoticeService {

    @Resource
    DoctorNoticeRepository doctorNoticeRepository;

    @Resource
    UserUtils userUtils;

    @Override
    public Map<String,Object>  List(NoticeListReq req) {
        Map<String,Object> result = new HashMap<>();
        List<DoctorNoticeRsp> doctorNoticeRsps = new ArrayList<>();
        long count = doctorNoticeRepository.count(userUtils.getUser().getDcId());
        List<DoctorNotice> doctorNotices = doctorNoticeRepository.list(userUtils.getUser().getDcId(),(req.getPage()-1)*req.getLimit(),req.getLimit());
        for (DoctorNotice doctorNotice : doctorNotices) {
            DoctorNoticeRsp doctorNoticeRsp = new DoctorNoticeRsp();
            doctorNoticeRsp.setID(doctorNotice.getId_bigint());
            doctorNoticeRsp.setContents(doctorNotice.getContents_nvarchar());
            doctorNoticeRsp.setCreateTime(doctorNotice.getCreate_time_datetime());
            doctorNoticeRsps.add(doctorNoticeRsp);
        }
        result.put("total",count);
        result.put("list",doctorNoticeRsps);
        return result;
    }

    @Override
    public void add(Add add) {
        DoctorNotice doctorNotice =new DoctorNotice();
        doctorNotice.setId_bigint(IdUtils.generateId());
        doctorNotice.setDc_id_bigint(userUtils.getUser().getDcId());
        doctorNotice.setContents_nvarchar(add.getContents());
        doctorNotice.setCreate_time_datetime(new Date());
        doctorNoticeRepository.save(doctorNotice);
    }

    @Override
    public void Edit(Edit edit) {
        DoctorNotice doctorNotice = doctorNoticeRepository.findById(edit.getID()).orElse(null);
        doctorNotice.setCreate_time_datetime(new Date());
        doctorNotice.setContents_nvarchar(edit.getContents());
        doctorNoticeRepository.save(doctorNotice);
    }

    @Override
    public void Delete(Delete delete) {
        doctorNoticeRepository.deleteById(delete.getID());
    }
}
